package com.linrung.sf.scheduler.utils;

import java.util.Date;
import java.util.List;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
import net.sf.json.JsonConfig;
import net.sf.json.processors.DefaultDefaultValueProcessor;

public class JsonObjectUtils {

	public static boolean isJson(String str){
		try{
			parseObject(str);
			return true;
		}catch(Exception ex){
			return false;
		}
	}

	public static JsonConfig getJsonConfig() {
		JsonConfig jsonConfig = new JsonConfig();
		jsonConfig.registerDefaultValueProcessor(Integer.class,
				new DefaultDefaultValueProcessor() {
					public Object getDefaultValue(Class type) {
						return null;
					}
				});
		jsonConfig.registerDefaultValueProcessor(Double.class,
				new DefaultDefaultValueProcessor() {
					public Object getDefaultValue(Class type) {
						return null;
					}
				});
		jsonConfig.registerJsonValueProcessor(Date.class,
				new JsonDateValueProcessor());
		return jsonConfig;
	}

	public static JSONObject parseObject(Object obj) {
		return JSONObject.fromObject(obj, getJsonConfig());
	}

	public static <T> JSONArray parseArray(List<T> list) {
		return JSONArray.fromObject(list, getJsonConfig());
	}
}
